<template>
  <a-modal
    title="修改通道配置参数"
    :maskClosable="false"
    :width="1050"
    placement="right"
    :closable="true"
    @cancel="onClose"
    :visible="pDstChannelCfgEditVisiable"
  >
    <a-form :form="form">
      <a-row>
        <a-col :md="12" :sm="24">
          <a-form-item label="通道标识" v-bind="formItemLayout">
            <a-input :disabled="true" v-decorator="['dstChannelName']" />
            <a-input hidden="true" v-decorator="['dstChannelId']" />
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="24">
          <a-form-item label="通道类型" v-bind="formItemLayout">
            <a-input :disabled="true" v-decorator="['dstChannelType']" />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :md="12" :sm="24">
          <a-form-item label="通道类型描述" v-bind="formItemLayout">
            <a-input
              v-decorator="[
                'dstChannelTypeDesc',
                {
                  relus: [{ required: true, message: '必填' }],
                },
              ]"
            /> </a-form-item
        ></a-col>
        <a-col :md="12" :sm="24">
          <a-form-item label="渠道号" v-bind="formItemLayout">
            <a-input v-decorator="['channelNo']" /> </a-form-item
        ></a-col>
      </a-row>
      <a-row>
        <a-col :md="12" :sm="24"
          ><a-form-item label="服务商商户号" v-bind="formItemLayout">
            <a-input v-decorator="['providerMchntNo']" /> </a-form-item
        ></a-col>
        <a-col :md="12" :sm="24">
          <a-form-item label="主APPID" v-bind="formItemLayout">
            <a-input v-decorator="['appId']" /> </a-form-item
        ></a-col>
      </a-row>
      <a-row>
        <a-col :md="12" :sm="24"
          ><a-form-item label="子APPID" v-bind="formItemLayout">
            <a-input v-decorator="['subAppId']" /> </a-form-item
        ></a-col>
      </a-row>
    </a-form>
    <div class="drawer-bootom-button">
      <a-button @click="handleSubmit" type="primary" :loading="loading"
        >保存</a-button
      >
      <a-button @click="onClose" style="margin-left: 0.8rem">取消</a-button>
    </div>
  </a-modal>
</template>
<script>
import { mapState, mapMutations } from "vuex";

const formItemLayout = {
  labelCol: { span: 6 },
  wrapperCol: { span: 12 },
};
export default {
  name: "PDstChannelCfgEdit",
  props: {
    pDstChannelCfgEditVisiable: {
      default: false,
    },
  },
  data() {
    return {
      formItemLayout,
      form: this.$form.createForm(this),
      roleData: [],
      userId: "",
      loading: false,
    };
  },
  computed: {
    ...mapState({
      currentUser: (state) => state.account.user,
    }),
  },
  methods: {
    ...mapMutations({
      setUser: "account/setUser",
    }),
    onClose() {
      this.loading = false;
      this.form.resetFields();
      this.$emit("close");
    },
    setFormValues({ ...list }) {
      let fields = [
        "dstChannelName",
        "dstChannelType",
        "dstChannelTypeDesc",
        "channelNo",
        "providerMchntNo",
        "appId",
        "subAppId",
      ];
      Object.keys(list).forEach((key) => {
        if (fields.indexOf(key) !== -1) {
          this.form.getFieldDecorator(key);
          let obj = {};
          obj[key] = list[key];
          this.form.setFieldsValue(obj);
        }
      });
    },

    handleSubmit() {
      this.form.validateFields((err, values) => {
        if (!err) {
          this.loading = true;
          let user = this.form.getFieldsValue();
          user.roleId = user.roleId.join(",");
          user.userId = this.userId;
          user.deptId = this.userDept;
          this.$put("user", {
            ...user,
          })
            .then((r) => {
              this.loading = false;
              this.$emit("success");
              // 如果修改用户就是当前登录用户的话，更新其state
              if (user.username === this.currentUser.username) {
                this.$get(`user/${user.username}`).then((r) => {
                  this.setUser(r.data);
                });
              }
            })
            .catch(() => {
              this.loading = false;
            });
        }
      });
    },
  },
  watch: {},
};
</script>
